package cn.com.css.taxInspection.attention.service.impl;

import cn.com.css.taxInspection.attention.entity.JcAyqksmEntity;
import cn.com.css.taxInspection.attention.entity.JcAyqksmmxEntity;
import cn.com.css.taxInspection.attention.mapper.JcAyqksmMapper;
import cn.com.css.taxInspection.attention.mapper.JcAyqksmmxMapper;
import cn.com.css.taxInspection.attention.service.JcAyqksmService;
import cn.com.css.taxInspection.attention.service.JcAyqksmmxService;
import cn.com.css.taxInspection.common.constants.Constants;
import cn.com.css.taxInspection.util.TsBqUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
@Slf4j
public class JcAyqksmmxServiceImpl extends ServiceImpl<JcAyqksmmxMapper, JcAyqksmmxEntity> implements JcAyqksmmxService {


    @Resource
    private JcAyqksmmxService jcAyqksmmxService;
    @Resource
    private JcAyqksmService jcAyqksmService;
    @Resource
    private JcAyqksmMapper jcAyqksmMapper;
    @Resource
    private JcAyqksmmxMapper jcAyqksmmxMapper;

    @Override
    public int saveJcAyqksmmx(List<JcAyqksmmxEntity> jcAyqksmmxList) {
        int result = 0;
        String jcajxxuuid = jcAyqksmmxList.get(0).getJcajxxuuid();
        //如果线索不是空
        JcAyqksmEntity jcAyqksmEntity = jcAyqksmMapper.selectById(jcajxxuuid);
        if (!jcAyqksmmxList.get(0).getBh().equals(Constants.AJZT_LX_N)) {
            // 根据UUID查询线索关联案件
            // 如果案源类型是Y
            if (Constants.AJZT_LX_Y.equals(jcAyqksmEntity.getBz())) {
                //先删除原来的线索
                jcAyqksmmxMapper.deleteByJcajxxuuid(jcAyqksmmxList.get(0).getJcajxxuuid());
                String aybz = jcAyqksmmxList.get(0).getAybz();
                if ("jc".equals(aybz) || "sl".equals(aybz)) {
                    long count = jcAyqksmmxList.stream()
                            .filter(item -> TsBqUtils.isNotNull(item.getSource()))
                            .count();
                    if (count > 0) {
                        if ("jc".equals(aybz)) {
                            jcAyqksmEntity.setJcbz(Constants.AJZT_LX_Y);
                        } else {
                            jcAyqksmEntity.setSlbz(Constants.AJZT_LX_Y);
                        }
                        jcAyqksmService.updateById(jcAyqksmEntity);
                    }
                }
            } else {
                //如果是N，就是第一次保存，就更新线索状态
                jcAyqksmEntity.setBz(Constants.AJZT_LX_Y);
                jcAyqksmService.updateById(jcAyqksmEntity);
            }
            //写入线索
            for (int i = 0; i < jcAyqksmmxList.size(); i++) {
                JcAyqksmmxEntity jcAyqksmmxEntity = jcAyqksmmxList.get(i);
                jcAyqksmmxEntity.setUuid(TsBqUtils.getUUID());
                jcAyqksmmxEntity.setBh(String.valueOf(i));
                result += jcAyqksmmxMapper.insert(jcAyqksmmxEntity);
            }
        } else {
            JcAyqksmEntity jcAyqksmEntity1 = new JcAyqksmEntity();
            jcAyqksmEntity1.setJcajxxuuid(jcajxxuuid);
            jcAyqksmEntity1.setBz(Constants.AJZT_LX_N);
            jcAyqksmEntity1.setJcbz(Constants.AJZT_LX_N);
            jcAyqksmEntity1.setSlbz(Constants.AJZT_LX_N);
            jcAyqksmService.updateById(jcAyqksmEntity1);
            jcAyqksmmxMapper.deleteByJcajxxuuid(jcajxxuuid);
        }
        return result;
    }

    @Override
    public List<JcAyqksmmxEntity> queryAyqksmXsByJcajxxuuid(String jcajxxuuid) {
        LambdaQueryWrapper<JcAyqksmmxEntity> queryWrapper = Wrappers.lambdaQuery();
        //案件编号
        if (TsBqUtils.isNotNull(jcajxxuuid)) {
            queryWrapper.eq(JcAyqksmmxEntity::getJcajxxuuid, jcajxxuuid);
        }
        queryWrapper.orderByAsc(JcAyqksmmxEntity::getBh);
        return jcAyqksmmxService.list(queryWrapper);

    }
}
